<template>
  <div class="Login">
    <!-- 登录logo图 -->
    <div class="login-logo"><div></div></div>
    <!-- 登录 -->
    <div class="login-body">
      <div class="login-phone">
        <label for="male"><span></span></label>
        <input type="number" ref="male" id="male" placeholder="手机号码" />
      </div>
      <div class="login-password">
        <label for="pass"><span class="pass-img"></span></label>
        <input type="password" ref="pass" id="pass" placeholder="密码" />
        <span :class="loginPass" @click="closePass(loginPass)"></span>
      </div>
      <!-- 注册 忘记密码 -->
      <div class="forget-pass">
        <span class="box_left" @click="toRegister">立即注册</span>
        <span class="box_right" @click="toForget">忘记密码？</span>
      </div>
      <!-- 登录按钮 -->
      <div class="login-button" @click="getLogin">登录</div>
      <!-- 协议 -->
      <div class="agreement">
        <span
          :class="radioClose"
          id="radio"
          checked="checked"
          @click="closeRadio(radioClose)"
        ></span>
        <span>
          <span @click="closeRadio(radioClose)">请先阅读并同意</span>
          <span style="color: #ff0000;" @click="toAgreement"
            >《用户隐私协议》</span
          >
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import { Indicator } from "mint-ui";
export default {
  data() {
    return {
      loginPass: "close-pass",
      radioClose: "radio-open",
      dealRadio: true
    };
  },
  methods: {
    closePass(val) {
      if (val == "close-pass") {
        this.loginPass = "open-pass";
        document.getElementById("pass").setAttribute("type", "text");
      }
      if (val == "open-pass") {
        this.loginPass = "close-pass";
        document.getElementById("pass").setAttribute("type", "password");
      }
    },
    closeRadio(val) {
      if (val == "radio-open") {
        this.radioClose = "radio-close";
        document.getElementById("radio").setAttribute("checked", "");
        this.dealRadio = false;
      }
      if (val == "radio-close") {
        this.radioClose = "radio-open";
        document.getElementById("radio").setAttribute("checked", "checked");
        this.dealRadio = true;
      }
    },
    toRegister() {
      this.$router.push({ name: "Register" });
    },
    toForget() {
      this.$router.push({ name: "ForgotPass" });
    },
    //协议
    toAgreement() {
      this.$router.push({ name: "Agreement" });
    },
    getLogin() {
      //登录
      var phone = this.$refs.male.value;
      var password = this.$refs.pass.value;
      if (this.dealRadio === false) {
        Toast("请阅读用户协议");
        return false;
      }
      let me = this;
      Indicator.open({ spinnerType: "fading-circle" });
      this.$axios
        .post(
          "/api/index/login",
          {
            mobile: phone,
            password: password
          },
          { emulateJSON: true }
        )
        .then(res => {
          console.log(res);
          Toast(res.msg);
          if (res.code == 400) {
            me.toRegister();
          } else if (res.code == 200) {
            sessionStorage.clear();
            sessionStorage.setItem("uid", res.data.id);
            sessionStorage["token"] = res.data.token;
            me.$store.commit("$_setStorage", res.data.id);
            me.$store.commit("setToken", res.data.token);
            me.$store.commit("$_setLogin", "1");
            me.$router.push({ name: "RootView" });
          }
          Indicator.close();
        })
        .catch(function(error) {
          console.log("报错：" + error);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.box_left {
  float: left;
}
.box_right {
  float: right;
}
//logo图
.login-logo {
  width: 8.4375rem;
  height: 8.4375rem;
  margin: 5.3125rem auto 0;
  border-radius: 4.375rem;

  :first-child {
    width: 100%;
    height: 100%;
    background-image: url(../../assets/login/logo.png);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    overflow: hidden;
  }
}
//登录
.login-body {
  width: 18.75rem;
  margin: 4.875rem auto 0;

  //手机号
  .login-phone {
    height: 2.1875rem;
    border-bottom: 1px solid #d6d6d6;

    span {
      width: 1.1875rem;
      height: 1.1875rem;
      display: block;
      float: left;
      background-image: url(../../assets/login/phone.png);
      background-repeat: no-repeat;
      background-size: contain;
      background-position: center;
      overflow: hidden;
    }
    input {
      float: left;
      width: 13.75rem;
      margin-left: 0.8125rem;
      font-size: 1.125rem;
      color: #191919;
      border: 0;
      outline: none;
      cursor: pointer;
    }
  }
  //密码
  .login-password {
    height: 2.1875rem;
    border-bottom: 1px solid #d6d6d6;
    margin-top: 2.375rem;

    .pass-img {
      width: 1.1875rem;
      height: 1.1875rem;
      display: block;
      float: left;
      background-image: url(../../assets/login/pass.png);
      background-repeat: no-repeat;
      background-size: contain;
      background-position: center;
      overflow: hidden;
    }
    input {
      float: left;
      width: 13.75rem;
      margin-left: 0.8125rem;
      font-size: 1.125rem;
      color: #191919;
      border: 0;
      outline: none;
      cursor: pointer;
    }
    .close-pass {
      margin-top: 0.3125rem;
      width: 1.125rem;
      height: 0.625rem;
      display: block;
      float: left;
      background-image: url(../../assets/login/pass-guan.png);
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      overflow: hidden;
    }
    .open-pass {
      margin-top: 0.3125rem;
      width: 1.125rem;
      height: 0.625rem;
      display: block;
      float: left;
      background-image: url(../../assets/login/pass-open.png);
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      overflow: hidden;
    }
  }
  .forget-pass {
    margin: 0.9375rem 0.625rem 0;
    height: 0.75rem;
    span {
      font-size: 0.75rem;
      color: #ff0000;
    }
  }
  // 登录按钮
  .login-button {
    width: 18.75rem;
    height: 3.0625rem;
    clear: both;
    margin-top: 1.25rem;
    border-radius: 1.5625rem;
    color: #ffffff;
    font-size: 1.125rem;
    text-align: center;
    line-height: 3.0625rem;

    /* 设置渐变 */
    background: rgb(255, 0, 0); /* Old browsers */
    background: -moz-linear-gradient(
      to right bottom,
      rgb(255, 127, 95) 10%,
      rgb(229, 39, 77) 70%
    ); /* FF3.6-15 */
    background: -webkit-linear-gradient(
      to right bottom,
      rgb(255, 127, 95) 10%,
      rgb(229, 39, 77) 70%
    ); /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(
      to right bottom,
      rgb(255, 127, 95) 30%,
      rgb(229, 39, 77) 80%
    ); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ff0000', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */
  }

  //协议
  .agreement {
    color: #4d4d4d;
    font-size: 0.875rem;
    margin-top: 1.8125rem;
    text-align: center;

    .radio-close {
      width: 0.75rem;
      height: 0.75rem;
      border: 0.0625rem solid #999999;
      display: inline-block;
      margin-right: 0.625rem;
      border-radius: 0.375rem;
      position: relative;
      top: 0.125rem;
    }
    .radio-open {
      width: 0.75rem;
      height: 0.75rem;
      border: 0.0625rem solid #ff0000;
      display: inline-block;
      margin-right: 0.625rem;
      border-radius: 0.375rem;
      position: relative;
      top: 0.125rem;
      background-color: #ff0000;
      background-image: url(../../assets/login/gou.png);
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      overflow: hidden;
    }
  }
}
</style>
